package com.oa.role.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oa.entity.ExtMsgResult;
import com.oa.entity.ExtPageable;
import com.oa.entity.Result;
import com.oa.log.annotation.Log;
import com.oa.role.dto.RoleDto;
import com.oa.role.service.RoleManagerService;
import com.oa.system.entity.Group;
import com.oa.system.entity.Role;
import com.oa.user.dto.UserInfoDto;

@Controller
@RequestMapping("/role")
public class RoleManagerController {

	@Resource
	private RoleManagerService roleManagerService;

	// role/allGroup 返回全部权限
	@GetMapping("/allGroup")
	public @ResponseBody Result<Iterable<Group>> findAllGroup() {
		return new Result<>(200, "查询全部权限", roleManagerService.findAllGroup());
	}

	// role/allRole 返回全部角色
	@GetMapping("/allRole")
	public @ResponseBody Result<Iterable<Role>> findAllRole() {
		return new Result<>(200, "查询全部角色", roleManagerService.findAllRole());
	}

	// role/searchAllRole 查询角色
	@GetMapping("/searchAllRole")
	public @ResponseBody Page<Role> searchAllRole(RoleDto roleDto, ExtPageable pageable) {
		return roleManagerService.searchAllRole(RoleDto.getRoleGroupQuery(roleDto), pageable.getPageable());
	}

	// role/searchAllUser 查询用户角色
	@GetMapping("/searchAllUser")
	public @ResponseBody Page<RoleDto> searchAllUser(UserInfoDto userInfoDto, ExtPageable pageable) {
		return roleManagerService.searchAllUser(userInfoDto, pageable.getPageable());
	}

	// role/saveOrUpdateRole 保存或更新角色
	@PostMapping("/saveOrUpdateRole")
	@Log(oprate = "增加或更新角色")
	@PreAuthorize("hasAnyAuthority('ADMIN','HIGH_MANAGER')")
	public @ResponseBody ExtMsgResult saveOrUpdateRole(Role role, @RequestParam("groups") List<Long> groups) {
		try {
			roleManagerService.saveOrUpdateRole(role, groups);
			return new ExtMsgResult(200, "success");
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtMsgResult(400, "error");
		}
	}

	// role/updateUserRole 更新用户角色
	@PostMapping("/updateUserRole")
	@Log(oprate = "更新员工角色")
	@PreAuthorize("hasAnyAuthority('ADMIN','HIGH_MANAGER')")
	public @ResponseBody ExtMsgResult updateUserRole(Long id, Long roleId) {
		try {
			roleManagerService.updateUserRole(id, roleId);
			return new ExtMsgResult(200, "success");
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtMsgResult(400, "error");
		}
	}

	// role/deleteRole 删除角色
	@PostMapping("/deleteRole")
	@Log(oprate = "删除角色")
	@PreAuthorize("hasAnyAuthority('ADMIN','HIGH_MANAGER')")
	public @ResponseBody ExtMsgResult deleteRole(Long id) {
		try {
			roleManagerService.deleteRole(id);
			return new ExtMsgResult(200, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtMsgResult(400, "删除失败");
		}
	}

	// role/deleteRoles 删除多个角色
	@PostMapping("/deleteRoles")
	@Log(oprate = "删除角色")
	@PreAuthorize("hasAnyAuthority('ADMIN','HIGH_MANAGER')")
	public @ResponseBody ExtMsgResult deleteUsers(@RequestParam("ids") List<Long> ids) {
		try {
			roleManagerService.deleteRole(ids);
			return new ExtMsgResult(200, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtMsgResult(400, "删除失败");
		}
	}
}
